var AldeasInfantiles = angular.module("AldeasInfantiles", []);

AldeasInfantiles.controller("Housing", function($scope, $http, $timeout) {

  $scope.countries = []; //Array with all registred countries in the database
  $scope.dorps = []; //Array with all registred dorps in the database
  $scope.houses = []; //Array with all registred houses in the database
  $scope.hiddenClass = "hidden"; //Used to show or hide the error message in the UI
  $scope.alertClass = ""; //Used to display a message in the UI
  $scope.errorMessage = ""; //Used to change the class of the message div in the UI
  $scope.house = {}; //Donor object
  $scope.selectedCountry = ""; //Selected donor country value
  $scope.selectedDorp = ""; //Selected donor country value

  //Requests all countries
  $scope.requestCountries = function () {
    $http({
          async: false,
          method: 'POST', 
          url: currentPath + 'countries/returncountries/'
        }).
        success(function(data) {
          $scope.countries = data;
          $scope.selectedCountry = data[0].id_pais;
          $scope.requestDorpsByCountry();
        }).
        error(function(data, status) {
          console.log(e.message);
        });
  };

  //Displays all dorps in the UI
  $scope.requestDorpsByCountry = function () {
    $http({
            method: 'POST', 
            url: currentPath + 'dorps/returndorpsbycountry/' + $scope.selectedCountry
        }).
        success(function(data) {
          $scope.dorps = data;
          $scope.selectedDorp = data[0].id_programa;
        }).
        error(function(data, status) {
           console.log(e.message);
        });
  };

  //Displays all houses in the UI
  $scope.showHouses = function () {
    $http({
            method: 'POST', 
            url: currentPath + 'housing/returnhousesbydorp/' + $scope.selectedDorp
        }).
        success(function(data) {
          $scope.houses = data;
        }).
        error(function(data, status) {
           console.log(e.message);
        });
  };

  //Receives the id of an user to get the information from the DB
  $scope.getHouseById = function (house) {
    var id = house.id_casa;
    $http({
            method: 'POST', 
            url: currentPath + 'housing/returnhousebyid/' + id
        }).
        success(function(data) {
          $scope.house.houseid = data[0].id_casa;
          $scope.house.housenum = data[0].ds_casa;
          $scope.house.detail = data[0].ds_detalle;
          $('#modal-houses').modal('hide');
        }).
        error(function(data, status) {
           console.log(e.message);
        });
  };

  $scope.getParams = function ($house) {
    $params = $.param({
      'houseid': $house.houseid,
      'housenum': $house.housenum,
      'detail': $house.detail,
      'housedorp': $scope.selectedDorp
    })
    $scope.saveHouse($params);
  };

  //Saves the user information in the database
  $scope.saveHouse = function ($params) {
    $http({
            headers: {'Content-Type': 'application/x-www-form-urlencoded'},
            url: currentPath + 'housing/savehouse/',
            method: 'POST',
            data: $params,
            
        }).
        success(function(data, status) {
          if(data.response == "success"){
            $scope.errorMessage = "Datos guardatos correctamente";
            $scope.hiddenClass = "";
            $scope.alertClass = "alert-success";
            $scope.resetForm();
          }else if(data.response == "incomplete_form"){
            $scope.hiddenClass = "";
            $scope.alertClass = "alert-danger";
            $scope.errorMessage = "Debes escribir todos los datos del formulario.";
          }else if(data.response == "failed"){
            $scope.hiddenClass = "";
            $scope.alertClass = "alert-danger";
            $scope.errorMessage = "Algo fallo al guardar los datos, intentalo de nuevo.";
          }
        }).
        error(function(data, status) {
          $scope.hiddenClass = "";
          $scope.errorMessage = data;
        });
  };

  $scope.resetForm = function() {
    $scope.house.houseid = "";
    $scope.house.housenum = "";
    $scope.house.detail = "";
    $scope.selectedCountry = 1;
    $timeout(countUp, 5000);
  };

  var countUp = function() {
    $scope.hiddenClass = "hidden";
  }

  $scope.setDefaultSettings = function () {
    $scope.requestCountries(); 
    //$scope.requestTypeUsers();
    //$scope.adduserstate = 1;
  }

  $scope.setDefaultSettings();

});